import os
from getID import *
from history2list import history2list
from translate_tree import translate_tree

def preliminary(the_id):
    getID_without_time(the_id + "_raw.json")
    #now we have sourcedata/sparse.txt and sourcedata/matrixID.json
    os.system("mv sourcedata/sparse.txt dat/sparse.txt")
    os.system("mv sourcedata/vector.txt dat/vector.txt")
    os.system("echo \"import adaptVEGAS as A;A.Adaptor4VEGAS('sparse.txt','vector.txt','result',[40,35,30,25,20,15,10,5],1,True,True,False,True)\" | python3")
    # now we have result.*.txt
    os.system("rm dat/sparse.txt;rm dat/vector.txt")
    os.system("mv sourcedata/matrixID.json sourcedata/" + the_id + "_matrixID.json")
    listeddir = os.listdir("out")
    target_file_name = ""
    for fname in listeddir:
        if "hist" in fname:
            target_file_name = fname
            break
    target_file_name = "out/" + target_file_name
    history2list(target_file_name)
    level2clusters = ['5', '10', '15', '20', '25', '30', '35', '40']
    for c in [0, 1, 2, 3, 4, 5, 6, 7]:
        os.system("mv " + str(c) + " sourcedata/" + the_id + "_" + level2clusters[c] + ".txt")
    translate_tree(the_id, target_file_name)
    os.system("rm out/*")
    print("preliminary part finished")
